package com.lyz.dataStructure.algorithm.小米2020;/**
 * @Author:759057893@qq.com Lyz
 * @Date: ${Date} 19:48
 * @Description:
 **/

import java.util.Scanner;

/**
 *@Author:759057893@qq.com Lyz
 *@Date: 2019/9/11 19:48
 *@Description:
 **/
public class 利润最大化 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        String[] st = s.split(" ");
        int[] arr = new int[st.length];
        for(int i =0;i<arr.length;i++){
            arr[i] = Integer.parseInt(st[i]);
        }
        System.out.println(maxProfit(arr));
    }

    public static int maxProfit(int[] arr){
        int len = arr.length;
        if(len<=1){
            return 0;
        }
        int[] maxFormer = new int[len];
        int[] maxLatter = new int[len];
        int day,diff,minPrice,maxPrice,maxProfit;

        minPrice = arr[0];
        maxProfit = 0;
        maxFormer[0] = 0;
        for(day=1;day<len;day++){
            diff= arr[day]-minPrice;
            if(diff<0){
                minPrice = diff;
            }else if(diff>maxProfit){
                maxProfit = diff;
            }
            maxFormer[day] = maxProfit;
        }
        maxPrice = arr[len-1];
        maxProfit = 0;
        maxLatter[len-1]=0;
        for(day=len-2;day>-1;day--){
            diff=maxPrice-arr[day];
            if(diff<0)
                maxPrice = arr[day];
            else if (diff>maxProfit)
                maxProfit = diff;
            maxLatter[day] = maxProfit;
        }
        int sum;
        maxProfit = 0;
        for(day=0;day<len;day++){
            sum=maxFormer[day]+maxLatter[day];
            if(sum>maxProfit)
                maxProfit = sum;
        }
        return maxProfit;
    }

}
